<template>
    <div class="container">
        <div class="operateblock">
            <el-form :inline="true" :model="state.formInline" class="demo-form-inline" ref="operateRef">
                <el-form-item prop="searchVal">
                    <el-input v-model="state.formInline.searchVal" placeholder="用户名 / 邮箱" />
                </el-form-item>
                <el-form-item label="用户状态" prop="state">
                    <el-select v-model="state.formInline.state">
                        <el-option label="全部" :value="0" />
                        <el-option label="在职" :value="1" />
                        <el-option label="离职" :value="2" />
                        <el-option label="试用期" :value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :icon="Search" @click="initData">查询</el-button>
                    <el-button :icon="Refresh" @click="reset(operateRef, initData)">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="tableblock">
            <div class="crudblock">
                <el-button type="primary" :icon="Plus" @click="handleDialog('新增用户')">新增</el-button>
                <el-popconfirm width="200px" @confirm="deleteFn(state.delIDArray)" confirm-button-text="Yes" :icon="InfoFilled" icon-color="#F56C6C" title="确认是否删除?">
                    <template #reference>
                        <el-button type="danger" :icon="Delete">批量删除</el-button>
                    </template>
                </el-popconfirm>
            </div>
            <el-table :data="tableData" @selection-change="handleSelec" v-loading="loading">
                <el-table-column type="selection" />
                <el-table-column prop="id" label="用户ID" align="center" min-width="100px" />
                <el-table-column prop="username" label="用户名" align="center" min-width="150px" />
                <el-table-column prop="email" label="邮箱" align="center" min-width="180px" />
                <el-table-column prop="role" label="角色" align="center" min-width="100px">
                    <template #default="scope">
                        <span>{{ scope.row.role == 0 ? '系统管理员' : '普通用户' }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="state" label="状态" align="center" min-width="80px">
                    <template #default="scope">
                        <span>{{ scope.row.state == 1 ? '在职' : scope.row.state == 2 ? '离职' : '试用期' }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="create_time" label="注册时间" align="center" min-width="165px">
                    <template #default="scope">
                        <span>{{ formatDate(scope.row.create_time) }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="last_login_time" label="最后登录时间" align="center" min-width="165px">
                    <template #default="scope">
                        <span>{{ formatDate(scope.row.last_login_time) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" min-width="200px" fixed="right">
                    <template #default="scope">
                        <el-button :icon="Edit" @click="handleDialog('编辑用户', scope.row)">编辑</el-button>
                        <el-popconfirm width="200px" @confirm="deleteFn(scope.row.id)" confirm-button-text="Yes" :icon="InfoFilled" icon-color="#F56C6C" title="确认是否删除?">
                            <template #reference>
                                <el-button type="danger" :icon="Delete">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="prev, pager, next" :total="total" v-model:current-page="curPage" v-model:page-size="pageSize" />
        </div>

        <!-- 表单对话框 -->
        <formDialog v-model:dialogState="dialogState" :dialogConfig="dialogConfig" :formConfig="formConfig" />
    </div>
</template>

<script setup>
import { Delete, Edit, Search, Plus, Refresh, InfoFilled } from '@element-plus/icons-vue'
import { reactive, ref, computed, onBeforeUnmount } from 'vue'
import { userList, userDelete, userCreate, userUpdate } from '@/api/index.js'
import formDialog from '@/components/formDialog.vue'
import { formatDate } from '@/utils/utils.js'
import useTableList from '@/hooks/useTableList.js'
import mitt from '@/utils/mitt'

const operateRef = ref(null)
const state = reactive({
    formInline: {
        searchVal: '',
        state: 0
    },
    delIDArray: []
})
const { loading, initData, tableData, total, curPage, pageSize, reset, dialogState, dialogConfig, handleDialog } = useTableList(userList, state.formInline)

// 监听表单提交事件
mitt.on('submit', res => {
    onSubmit(res)
})

// 创建 OR 编辑
const onSubmit = async (d) => {
    let res = dialogConfig.title == '新增用户' ? await userCreate(d) : await userUpdate(d)
    initData()
    dialogState.value = false
    ElMessage.success(res)
}

const handleSelec = (val) => {
    state.delIDArray = val.map(i => i.id)
}

// 删除 OR 批量删除
const deleteFn = async (id) => {
    if (Array.isArray(id) && id.length === 0) return ElMessage.error('请勾选用户')
    let res = await userDelete(id.length ? id : [id])
    initData()
    ElMessage.success(res)
}

const deptOptions = [
    {
        value: 'guide',
        label: 'Guide',
        children: [
            {
                value: 'disciplines',
                label: 'Disciplines',
                children: [
                    {
                        value: 'consistency',
                        label: 'Consistency',
                    },
                    {
                        value: 'feedback',
                        label: 'Feedback',
                    },
                    {
                        value: 'efficiency',
                        label: 'Efficiency',
                    },
                    {
                        value: 'controllability',
                        label: 'Controllability',
                    },
                ],
            },
            {
                value: 'navigation',
                label: 'Navigation',
                children: [
                    {
                        value: 'side nav',
                        label: 'Side Navigation',
                    },
                    {
                        value: 'top nav',
                        label: 'Top Navigation',
                    },
                ],
            },
        ],
    },
    {
        value: 'component',
        label: 'Component',
        children: [
            {
                value: 'basic',
                label: 'Basic',
                children: [
                    {
                        value: 'layout',
                        label: 'Layout',
                    },
                    {
                        value: 'color',
                        label: 'Color',
                    },
                    {
                        value: 'typography',
                        label: 'Typography',
                    },
                    {
                        value: 'icon',
                        label: 'Icon',
                    },
                    {
                        value: 'button',
                        label: 'Button',
                    },
                ],
            },
            {
                value: 'form',
                label: 'Form',
                children: [
                    {
                        value: 'radio',
                        label: 'Radio',
                    },
                    {
                        value: 'checkbox',
                        label: 'Checkbox',
                    },
                    {
                        value: 'input',
                        label: 'Input',
                    },
                    {
                        value: 'input-number',
                        label: 'InputNumber',
                    },
                    {
                        value: 'select',
                        label: 'Select',
                    },
                    {
                        value: 'cascader',
                        label: 'Cascader',
                    },
                    {
                        value: 'switch',
                        label: 'Switch',
                    },
                    {
                        value: 'slider',
                        label: 'Slider',
                    },
                    {
                        value: 'time-picker',
                        label: 'TimePicker',
                    },
                    {
                        value: 'date-picker',
                        label: 'DatePicker',
                    },
                    {
                        value: 'datetime-picker',
                        label: 'DateTimePicker',
                    },
                    {
                        value: 'upload',
                        label: 'Upload',
                    },
                    {
                        value: 'rate',
                        label: 'Rate',
                    },
                    {
                        value: 'form',
                        label: 'Form',
                    },
                ],
            },
            {
                value: 'data',
                label: 'Data',
                children: [
                    {
                        value: 'table',
                        label: 'Table',
                    },
                    {
                        value: 'tag',
                        label: 'Tag',
                    },
                    {
                        value: 'progress',
                        label: 'Progress',
                    },
                    {
                        value: 'tree',
                        label: 'Tree',
                    },
                    {
                        value: 'pagination',
                        label: 'Pagination',
                    },
                    {
                        value: 'badge',
                        label: 'Badge',
                    },
                ],
            },
            {
                value: 'notice',
                label: 'Notice',
                children: [
                    {
                        value: 'alert',
                        label: 'Alert',
                    },
                    {
                        value: 'loading',
                        label: 'Loading',
                    },
                    {
                        value: 'message',
                        label: 'Message',
                    },
                    {
                        value: 'message-box',
                        label: 'MessageBox',
                    },
                    {
                        value: 'notification',
                        label: 'Notification',
                    },
                ],
            },
            {
                value: 'navigation',
                label: 'Navigation',
                children: [
                    {
                        value: 'menu',
                        label: 'Menu',
                    },
                    {
                        value: 'tabs',
                        label: 'Tabs',
                    },
                    {
                        value: 'breadcrumb',
                        label: 'Breadcrumb',
                    },
                    {
                        value: 'dropdown',
                        label: 'Dropdown',
                    },
                    {
                        value: 'steps',
                        label: 'Steps',
                    },
                ],
            },
            {
                value: 'others',
                label: 'Others',
                children: [
                    {
                        value: 'dialog',
                        label: 'Dialog',
                    },
                    {
                        value: 'tooltip',
                        label: 'Tooltip',
                    },
                    {
                        value: 'popover',
                        label: 'Popover',
                    },
                    {
                        value: 'card',
                        label: 'Card',
                    },
                    {
                        value: 'carousel',
                        label: 'Carousel',
                    },
                    {
                        value: 'collapse',
                        label: 'Collapse',
                    },
                ],
            },
        ],
    },
    {
        value: 'resource',
        label: 'Resource',
        children: [
            {
                value: 'axure',
                label: 'Axure Components',
            },
            {
                value: 'sketch',
                label: 'Sketch Templates',
            },
            {
                value: 'docs',
                label: 'Design Documentation',
            },
        ],
    },
]

let formConfig = computed(() => {
    return {
        labelWidth: '80px',
        rules: {
            username: [
                { required: true, message: '不能为空!', trigger: 'blur' },
            ],
            email: [
                { required: true, message: '不能为空!', trigger: 'blur' },
            ],
            dept_id: [
                { required: true, message: '不能为空!', trigger: 'blur' },
            ],
        },
        items: [
            { label: '用户名', type: 'text', name: 'username', props: { 'disabled': dialogConfig.title == '编辑用户' ? true : false } },
            { label: '邮箱', type: 'text', name: 'email', props: { 'type': 'email', 'disabled': dialogConfig.title == '编辑用户' ? true : false } },
            { label: '手机号', type: 'text', name: 'mobile' },
            { label: '岗位', type: 'text', name: 'job' },
            {
                label: '性别',
                type: 'radio',
                name: 'sex',
                options: [
                    { label: '男', value: 0 },
                    { label: '女', value: 1 },
                ],
            },
            {
                label: '状态',
                type: 'select',
                name: 'state',
                defaultValue: 1,
                options: [
                    { label: '在职', value: 1 },
                    { label: '离职', value: 2 },
                    { label: '试用期', value: 3 },
                ],
                props: { 'placeholder': '请选择' }
            },
            {
                label: '系统角色',
                type: 'select',
                name: 'role_list',
                defaultValue: [],
                options: [
                    { label: '产品经理', value: 1 },
                    { label: '研发', value: 2 },
                    { label: '测试', value: 3 },
                    { label: '运维', value: 4 },
                    { label: '运营', value: 5 },
                    { label: '市场', value: 6 },
                    { label: '客服', value: 7 },
                ],
                props: { 'placeholder': '请选择', 'multiple': true, 'collapse-tags-tooltip': true, 'collapse-tags': true }
            },
            {
                label: '所属部门',
                type: 'cascader',
                name: 'dept_id',
                props: {
                    'options': deptOptions,
                    'props': { checkStrictly: true },
                    'clearable': true,
                    'placeholder': '请选择'
                }
            }
        ]
    }
})

onBeforeUnmount(() => {
    mitt.off('submit')
})

</script>

<style lang='scss' scoped></style>